package com.tsingteng.cosfun.widget.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.faceunity.beautycontrolview.OnFaceUnityControlListener;
import com.qiniu.pili.droid.shortvideo.PLBuiltinFilter;
import com.qiniu.pili.droid.shortvideo.PLShortVideoEditor;
import com.qiniu.pili.droid.shortvideo.PLShortVideoRecorder;
import com.tsingteng.cosfun.R;
import com.tsingteng.cosfun.callback.OnBeautyNoneListener;
import com.tsingteng.cosfun.helper.VideoHelper;
import com.tsingteng.cosfun.ui.costar.PreViewActivity;
import com.tsingteng.cosfun.utils.AppUtils;
import com.tsingteng.cosfun.utils.LogUtils;
import com.tsingteng.cosfun.utils.VideoFilterCommand;
import com.tsingteng.cosfun.widget.DiscreteSeekBar;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

import static android.view.View.VISIBLE;

/**
 * 描述 :
 * 创建人: 肖利.
 * 时间:2018/8/21
 * qq(872179966)
 */

public class BeautifyDialog implements View.OnClickListener {
    private int type = 1; //1是拍摄  2 是编辑
    private PLShortVideoRecorder mShortVideoRecorder;
    private PLShortVideoEditor mShortVideoEditor;
    private Activity context;
    private LinearLayout mLlLvJin;
    private LinearLayout mLlMeiHua;
    private LinearLayout mLlZiDingMeiHua;
    private TextView mTvLvJin;
    private TextView mTvLvJinLine;
    private TextView mTvMeiHua;
    private TextView mTvMeiHuaLine;
    private TextView mTvZiDingMeiHua;
    private TextView mTvZiDingMeiHuaLine;
    private RecyclerView rvRv;
    private LinearLayout mllMeiHua;
    private int position = -1;
    private TextView mTvZone;
    private TextView mTvOne;
    private TextView mTvTwo;
    private TextView mTvThree;
    private TextView mTvFour;
    private HorizontalScrollView mHsLj;
    private LinearLayout mllMeihua;
    private TextView mTvFive;
    private String mSelectedFilter;
    private int currentFPosition = -1;
    private int currentBPosition = -1;
    private FilterListAdapter filterListAdapter;

    private float mFaceBeautyBlurLevel = 0.6f;//磨皮
    private float mFaceBeautyColorLevel = 0.5f;//美白
    private float mBrightEyesLevel = 0.3f;//亮眼
    private float mFaceBeautyEnlargeEye = 0.3f;//大眼
    private float mFaceBeautyCheekThin = 0.5f;//瘦脸
    private float mChinLevel = 0.42f;//下巴
    private float mForeheadLevel = 0.40f;//额头
    private float mThinNoseLevel = 0.4f;//瘦鼻
    private float mMouthShape = 0.43f;//嘴形
    private Dialog dialog;
    private PLBuiltinFilter[] builtinFilterList;
    private final List<Integer> beautyList;
    private BeautyListAdapter beautyListAdapter;
    private final List<String> nameList;
    private FrameLayout mBeautySeekBarLayout;
    private DiscreteSeekBar mBeautySeekBar;

    private int beautyType = 0;
    private TextView mTvDefaultUp;
    private TextView mTvDefaultDown;
    private final List<Integer> pressBeautyList;
    private RecyclerView rvMh;
    private int beautyFilter = -1;
    private ArrayList<Float> selfBeautyList = new ArrayList<>();
    private int CurrentBeautyLevel = -1;
    private OnDialogDissMissListener onDialogDissMissListener;
    private OnBeautyNoneListener onBeautyNoneListener;


    public BeautifyDialog(Activity context, PLShortVideoRecorder mPlshortVideoRecorder, PLShortVideoEditor mShortVideoEditor, int type) {
        this.mShortVideoRecorder = mPlshortVideoRecorder;
        this.mShortVideoEditor = mShortVideoEditor;
        this.context = context;
        this.type = type;
        beautyList = new ArrayList<>();
        beautyList.add(R.drawable.skin_icon_nopress);
        beautyList.add(R.drawable.whitening_icon_nopress);
        beautyList.add(R.drawable.bright_eyes_icon_nopress);
        beautyList.add(R.drawable.big_eyes_icon_nopress);
        beautyList.add(R.drawable.face_lifting_icon_nopress);
        beautyList.add(R.drawable.chin_icon_nopress);
        beautyList.add(R.drawable.forehead_icon_nopress);
        beautyList.add(R.drawable.nose_nopress);
        beautyList.add(R.drawable.mouth_icon_nopress);
        nameList = new ArrayList<>();
        nameList.add("磨皮");
        nameList.add("美白");
        nameList.add("亮眼");
        nameList.add("大眼");
        nameList.add("瘦脸");
        nameList.add("下巴");
        nameList.add("额头");
        nameList.add("瘦鼻");
        nameList.add("嘴型");
        pressBeautyList = new ArrayList<>();
        pressBeautyList.add(R.drawable.skin_icon);
        pressBeautyList.add(R.drawable.whitening_icon);
        pressBeautyList.add(R.drawable.bright_eyes_icon);
        pressBeautyList.add(R.drawable.big_eyes_icon);
        pressBeautyList.add(R.drawable.face_lifting_icon);
        pressBeautyList.add(R.drawable.chin_icon);
        pressBeautyList.add(R.drawable.forehead_icon);
        pressBeautyList.add(R.drawable.nose_icon);
        pressBeautyList.add(R.drawable.mouth_icon);
        initView();
    }

    private void initView() {
        dialog = new Dialog(context, R.style.beautydialog);
        dialog.setContentView(R.layout.dialog_beautify);
        mLlLvJin = dialog.findViewById(R.id.ll_lj);
        mLlMeiHua = dialog.findViewById(R.id.ll_mh);
        mLlZiDingMeiHua = dialog.findViewById(R.id.ll_zd_mh);
        mTvLvJin = dialog.findViewById(R.id.tv_lj);
        mTvLvJinLine = dialog.findViewById(R.id.tv_lj_line);
        mTvMeiHua = dialog.findViewById(R.id.tv_mh);
        mTvMeiHuaLine = dialog.findViewById(R.id.tv_mh_line);
        mTvZiDingMeiHua = dialog.findViewById(R.id.tv_zd_mh);
        mTvZiDingMeiHuaLine = dialog.findViewById(R.id.tv_zd_mh_line);
        mHsLj = dialog.findViewById(R.id.hs_lj);
        mllMeihua = dialog.findViewById(R.id.ll_mei_hua);
        mTvZone = dialog.findViewById(R.id.tv_zone);
        mTvOne = dialog.findViewById(R.id.tv_one);
        mTvTwo = dialog.findViewById(R.id.tv_two);
        mTvThree = dialog.findViewById(R.id.tv_three);
        mTvFour = dialog.findViewById(R.id.tv_four);
        mTvFive = dialog.findViewById(R.id.tv_five);
        rvRv = dialog.findViewById(R.id.rv_lj);
        rvMh = dialog.findViewById(R.id.rv_mh);
        mTvDefaultUp = dialog.findViewById(R.id.tv_default_up);
        mTvDefaultDown = dialog.findViewById(R.id.tv_default_down);
        mllMeiHua = dialog.findViewById(R.id.ll_mei_hua);
        mBeautySeekBarLayout = (FrameLayout) dialog.findViewById(R.id.beauty_seek_bar_layout);
        mBeautySeekBar = (DiscreteSeekBar) dialog.findViewById(R.id.beauty_seek_bar);
        dialog.findViewById(R.id.ll_none).setOnClickListener(this);
        dialog.findViewById(R.id.iv_jt).setOnClickListener(this);
        mLlLvJin.setOnClickListener(this);
        mLlMeiHua.setOnClickListener(this);
        mLlZiDingMeiHua.setOnClickListener(this);
        mTvZone.setOnClickListener(this);
        mTvOne.setOnClickListener(this);
        mTvTwo.setOnClickListener(this);
        mTvThree.setOnClickListener(this);
        mTvFour.setOnClickListener(this);
        mTvFive.setOnClickListener(this);
        rvRv.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));
        rvMh.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));
        rvRv.setVisibility(View.VISIBLE);
        rvMh.setVisibility(View.GONE);
        if (type == 2) {
            mLlMeiHua.setVisibility(View.GONE);
            mLlZiDingMeiHua.setVisibility(View.GONE);
        } else {
            mLlMeiHua.setVisibility(VISIBLE);
            mLlZiDingMeiHua.setVisibility(VISIBLE);
        }
        changeUI(mTvLvJin, mTvLvJinLine);
        mHsLj.fullScroll(ScrollView.FOCUS_LEFT);
        initAdapter();
        position = 0;
        dialog.setOnDismissListener(dialog -> {
            if (onDialogDissMissListener != null) {
                onDialogDissMissListener.dissMissClick();
            }
        });
        mBeautySeekBar.setOnProgressChangeListener(new DiscreteSeekBar.OnProgressChangeListener() {
            @Override
            public void onProgressChanged(DiscreteSeekBar SeekBar, int value, boolean fromUser) {
                if (!fromUser) return;
                if (onBeautyNoneListener!=null) {
                    onBeautyNoneListener.onHaveBeautyClick();
                }
                if (selfBeautyList == null) {
                    selfBeautyList = new ArrayList<>();
                } else {
                    selfBeautyList.clear();
                }
                float valueF = 1.0f * (value - SeekBar.getMin()) / 100;
                LogUtils.i("mBeautySeekBar" + valueF);
                if (beautyType == 0) {//磨皮
                    mFaceBeautyBlurLevel = valueF;
                    if (mOnFaceUnityControlListener != null)
                        selfBeautyList.add(mFaceBeautyBlurLevel);
                    mOnFaceUnityControlListener.onBlurLevelSelected(mFaceBeautyBlurLevel);

                }
                if (beautyType == 1) {//美白
                    mFaceBeautyColorLevel = valueF;
                    selfBeautyList.add(mFaceBeautyColorLevel);
                    if (mOnFaceUnityControlListener != null)
                        mOnFaceUnityControlListener.onColorLevelSelected(mFaceBeautyColorLevel);

                }
                if (beautyType == 2) {//亮眼
                    selfBeautyList.add(mBrightEyesLevel);
                    mBrightEyesLevel = valueF;
                    if (mOnFaceUnityControlListener != null)
                        mOnFaceUnityControlListener.onBrightEyesSelected(mBrightEyesLevel);

                }
                if (beautyType == 3) {//大眼
                    mFaceBeautyBlurLevel = valueF;
                    selfBeautyList.add(mFaceBeautyBlurLevel);
                    if (mOnFaceUnityControlListener != null)
                        mOnFaceUnityControlListener.onEnlargeEyeSelected(mFaceBeautyBlurLevel);

                }
                if (beautyType == 4) {//瘦脸
                    mFaceBeautyCheekThin = valueF;
                    selfBeautyList.add(mFaceBeautyCheekThin);
                    if (mOnFaceUnityControlListener != null)
                        mOnFaceUnityControlListener.onCheekThinSelected(mFaceBeautyCheekThin);

                }
                if (beautyType == 5) {//下巴
                    selfBeautyList.add(mChinLevel);
                    mChinLevel = valueF;
                    if (mOnFaceUnityControlListener != null)
                        mOnFaceUnityControlListener.onChinLevelSelected(mChinLevel);

                }
                if (beautyType == 6) {//额头
                    selfBeautyList.add(mForeheadLevel);
                    if (mOnFaceUnityControlListener != null)
                        mOnFaceUnityControlListener.onForeheadLevelSelected(mForeheadLevel);
                    mForeheadLevel = valueF;
                }
                if (beautyType == 7) {//瘦鼻
                    mThinNoseLevel = valueF;
                    selfBeautyList.add(mThinNoseLevel);
                    if (mOnFaceUnityControlListener != null)
                        mOnFaceUnityControlListener.onThinNoseLevelSelected(mThinNoseLevel);

                }
                if (beautyType == 8) {//嘴型
                    selfBeautyList.add(mMouthShape);
                    mMouthShape = valueF;
                    if (mOnFaceUnityControlListener != null)
                        mOnFaceUnityControlListener.onMouthShapeSelected(mMouthShape);
                }
                //记录美颜的值
                AppUtils.setSelfBeautyLevel(selfBeautyList);

            }

            @Override
            public void onStartTrackingTouch(DiscreteSeekBar SeekBar) {

            }

            @Override
            public void onStopTrackingTouch(DiscreteSeekBar SeekBar) {

            }
        });
    }

    private void initSelfBeaufyList() {
        ArrayList<Float> selfBeautyLevel = AppUtils.getSelfBeautyLevel();
        if (selfBeautyLevel == null || selfBeautyLevel.size() == 0) {
            return;
        } else {
            mFaceBeautyBlurLevel = Float.valueOf(String.valueOf(selfBeautyLevel.get(0)));
            mFaceBeautyColorLevel = Float.valueOf(String.valueOf(selfBeautyLevel.get(1)));
            mBrightEyesLevel = Float.valueOf(String.valueOf(selfBeautyLevel.get(2)));
            mFaceBeautyEnlargeEye = Float.valueOf(String.valueOf(selfBeautyLevel.get(3)));
            mFaceBeautyCheekThin = Float.valueOf(String.valueOf(selfBeautyLevel.get(4)));
            mChinLevel = Float.valueOf(String.valueOf(selfBeautyLevel.get(5)));
            mForeheadLevel = Float.valueOf(String.valueOf(selfBeautyLevel.get(6)));
            mThinNoseLevel = Float.valueOf(String.valueOf(selfBeautyLevel.get(7)));
            mMouthShape = Float.valueOf(String.valueOf(selfBeautyLevel.get(8)));
            setAgument(1);
        }
    }

    public void show() {
        Window window = dialog.getWindow();
        window.setGravity(Gravity.BOTTOM);
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, AppUtils.getScreenWidthAndHeight(context)[1] * 32 / 100);
        window.setWindowAnimations(R.style.dialogWindowAnim);
        if (mHsLj!= null)
        mHsLj.fullScroll(ScrollView.FOCUS_LEFT);
//        initAdapter();
        dialog.show();
    }

    private void seekToSeekBar(float value) {
        seekToSeekBar(value, 0, 100);
    }

    private void seekToSeekBar(float value, int min, int max) {
        if (value < 1000) {
            mBeautySeekBarLayout.setVisibility(VISIBLE);
            mBeautySeekBar.setMin(min);
            mBeautySeekBar.setMax(max);
            mBeautySeekBar.setProgress((int) (value * (max - min) + min));
        }
    }

    public void changeUI(TextView name, TextView line) {
        mTvLvJin.setTextColor(context.getResources().getColor(R.color.white_alpha_60));
        mTvMeiHua.setTextColor(context.getResources().getColor(R.color.white_alpha_60));
        mTvZiDingMeiHua.setTextColor(context.getResources().getColor(R.color.white_alpha_60));
        name.setTextColor(context.getResources().getColor(R.color.white));
        mTvLvJinLine.setVisibility(View.INVISIBLE);
        mTvMeiHuaLine.setVisibility(View.INVISIBLE);
        mTvZiDingMeiHuaLine.setVisibility(View.INVISIBLE);
        line.setVisibility(VISIBLE);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_lj:
                if (AppUtils.getBeautyFilterLevel()!= -1) {
                    mTvDefaultUp.setBackground(context.getResources().getDrawable(R.drawable.shape_round_white70_bg));
                }else {
                    mTvDefaultUp.setBackground(context.getResources().getDrawable(R.drawable.shape_round_white_bg));
                }
                mHsLj.setVisibility(VISIBLE);
                mHsLj.fullScroll(ScrollView.FOCUS_LEFT);
                if (position == 0) {
                    return;
                }
                rvRv.setVisibility(View.VISIBLE);
                rvMh.setVisibility(View.GONE);
                mTvDefaultDown.setText("无");
                changeUI(mTvLvJin, mTvLvJinLine);
                initAdapter();
                position = 0;
                break;
            case R.id.ll_mh:
                mHsLj.setVisibility(View.GONE);
                if (position == 1) {
                    return;
                }
                changBeautyUI(null);
                int beautyParameterLevel = AppUtils.getBeautyParameterLevel();
                if (beautyParameterLevel != -1) {
                    setLevel(beautyParameterLevel);
                    switch (beautyParameterLevel) {
                        case 0:
                            changBeautyUI(mTvZone);
                            break;
                        case 1:
                            changBeautyUI(mTvOne);
                            break;
                        case 2:
                            changBeautyUI(mTvTwo);
                            break;
                        case 3:
                            changBeautyUI(mTvThree);
                            break;
                        case 4:
                            changBeautyUI(mTvFour);
                            break;
                        case 5:
                            changBeautyUI(mTvFive);
                            break;
                    }
                }
                mBeautySeekBarLayout.setVisibility(View.INVISIBLE);
                mllMeiHua.setVisibility(VISIBLE);
                changeUI(mTvMeiHua, mTvMeiHuaLine);
                position = 1;
                break;
            case R.id.ll_zd_mh:
                mHsLj.fullScroll(ScrollView.FOCUS_LEFT);
                mHsLj.setVisibility(VISIBLE);
                rvRv.setVisibility(View.GONE);
                rvMh.setVisibility(View.VISIBLE);
                mTvDefaultDown.setText("使用默认");
                changeUI(mTvZiDingMeiHua, mTvZiDingMeiHuaLine);
                initBeautyAdapter();
                mllMeiHua.setVisibility(View.GONE);
                position = 2;


                if (AppUtils.getBeautyParameterLevel()!=-1) {//
                    mTvDefaultUp.setBackground(context.getResources().getDrawable(R.drawable.shape_round_white70_bg));
                }else {
                    mTvDefaultUp.setBackground(context.getResources().getDrawable(R.drawable.shape_round_white_bg));
                }
                break;

            case R.id.ll_none:
                mBeautySeekBarLayout.setVisibility(View.INVISIBLE);
                if (position == 0) {
                    AppUtils.setBeautyFilterLevel(-1);
                    mSelectedFilter = null;
                    if (mShortVideoRecorder == null) {
                        mShortVideoEditor.setBuiltinFilter(null);
                    } else {
                        mShortVideoRecorder.setBuiltinFilter(null);
                    }
                    if (filterListAdapter != null && currentFPosition != -1) {
                        CircleImageView oldIcon = (CircleImageView) filterListAdapter.getViewByPosition(rvRv, currentFPosition, R.id.icon);
                        ImageView oldTag = (ImageView) filterListAdapter.getViewByPosition(rvRv, currentFPosition, R.id.iv_tag);
                        oldIcon.setBorderWidth(0);
                        oldTag.setVisibility(View.GONE);
                        currentFPosition = -1;
                    }
                    mTvDefaultDown.setText("无");
                }
                mTvDefaultUp.setBackground(context.getResources().getDrawable(R.drawable.shape_round_white_bg));
                if (position == 2) {
                    new ContentDialog(context).setContent("确定恢复所有美化效果到默认？").setAffirmContent("确定").setAffirmContentColor(R.color.red_bag_color).setCancelContent("取消").setOnTwoClickListener(new ContentDialog.OnTwoClickListener() {
                        @Override
                        public void onAffirm() {
                            if (beautyListAdapter != null && currentBPosition != -1) {
                                RelativeLayout oldmRlBeauty = (RelativeLayout) beautyListAdapter.getViewByPosition(rvMh, currentBPosition, R.id.rl_beauty);
                                ImageView oldIvBeauty = (ImageView) beautyListAdapter.getViewByPosition(rvMh, currentBPosition, R.id.iv_beauty);
                                oldmRlBeauty.setBackground(context.getResources().getDrawable(R.drawable.shape_round_white70_bg));
                                oldIvBeauty.setImageDrawable(context.getResources().getDrawable(beautyList.get(currentBPosition)));
                                currentBPosition = -1;
                            }
                            setLevel(-1);
                            mTvDefaultDown.setText("使用默认");
                        }

                        @Override
                        public void onCancel() {

                        }
                    }).show();
                }

                break;
            case R.id.iv_jt:
                dismiss();
                break;
            case R.id.tv_zone:
                if (CurrentBeautyLevel == 0) {
                    changBeautyUI(null);
                    setLevel(-1);
                    CurrentBeautyLevel = -1;
                } else {
                    setLevel(0);
                    changBeautyUI(mTvZone);
                    CurrentBeautyLevel = 0;
                }
                break;
            case R.id.tv_one:
                if (CurrentBeautyLevel == 1) {
                    changBeautyUI(null);
                    setLevel(-1);
                    CurrentBeautyLevel = -1;
                } else {
                    setLevel(1);
                    changBeautyUI(mTvOne);
                    CurrentBeautyLevel = 1;
                }
                break;
            case R.id.tv_two:
                if (CurrentBeautyLevel == 2) {
                    setLevel(-1);
                    CurrentBeautyLevel = -1;
                    changBeautyUI(null);
                } else {
                    setLevel(2);
                    changBeautyUI(mTvTwo);
                    CurrentBeautyLevel = 2;
                }

                break;
            case R.id.tv_three:
                if (CurrentBeautyLevel == 3) {
                    changBeautyUI(null);
                    setLevel(-1);
                    CurrentBeautyLevel = -1;
                } else {
                    setLevel(3);
                    changBeautyUI(mTvThree);
                    CurrentBeautyLevel = 3;
                }

                break;
            case R.id.tv_four:
                if (CurrentBeautyLevel == 4) {
                    setLevel(-1);
                    CurrentBeautyLevel = -1;
                    changBeautyUI(null);
                } else {
                    setLevel(4);
                    changBeautyUI(mTvFour);
                    CurrentBeautyLevel = 4;
                }

                break;
            case R.id.tv_five:
                if (CurrentBeautyLevel == 5) {
                    setLevel(-1);
                    CurrentBeautyLevel = -1;
                    changBeautyUI(null);
                } else {
                    setLevel(5);
                    changBeautyUI(mTvFive);
                    CurrentBeautyLevel = 5;
                }
                break;


        }
    }


    private void initBeautyAdapter() {

        beautyListAdapter = new BeautyListAdapter();
        beautyListAdapter.addData(beautyList);
        rvMh.setAdapter(beautyListAdapter);

        mBeautySeekBarLayout.setVisibility(View.INVISIBLE);
        beautyListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                mBeautySeekBarLayout.setVisibility(VISIBLE);
                beautyType = position;
                AppUtils.setSelfBeautyLevelClick(position);
                AppUtils.setBeautyParameterLevel(-1);
//                ToastUtils.showToast(nameList.get(position));
                if (position == 0) {//磨皮
                    seekToSeekBar(mFaceBeautyBlurLevel);
                    if (mOnFaceUnityControlListener != null)
                        mOnFaceUnityControlListener.onBlurLevelSelected(mFaceBeautyBlurLevel);
                }
                if (position == 1) {//美白
                    seekToSeekBar(mFaceBeautyColorLevel);
                    if (mOnFaceUnityControlListener != null)
                        mOnFaceUnityControlListener.onColorLevelSelected(mFaceBeautyColorLevel);
                }
                if (position == 2) {//亮眼
                    seekToSeekBar(mBrightEyesLevel);
                    if (mOnFaceUnityControlListener != null)
                        mOnFaceUnityControlListener.onBrightEyesSelected(mBrightEyesLevel);
                }
                if (position == 3) {//大眼
                    seekToSeekBar(mFaceBeautyEnlargeEye);
                    if (mOnFaceUnityControlListener != null)
                        mOnFaceUnityControlListener.onEnlargeEyeSelected(mFaceBeautyEnlargeEye);
                }
                if (position == 4) {//瘦脸
                    seekToSeekBar(mFaceBeautyCheekThin);
                    if (mOnFaceUnityControlListener != null)
                        mOnFaceUnityControlListener.onCheekThinSelected(mFaceBeautyCheekThin);
                }
                if (position == 5) {//下巴
                    seekToSeekBar(mChinLevel, -50, 50);
                    if (mOnFaceUnityControlListener != null)
                        mOnFaceUnityControlListener.onChinLevelSelected(mChinLevel);
                }
                if (position == 6) {//额头
                    seekToSeekBar(mForeheadLevel, -50, 50);
                    if (mOnFaceUnityControlListener != null)
                        mOnFaceUnityControlListener.onForeheadLevelSelected(mForeheadLevel);
                }
                if (position == 7) {//瘦鼻
                    seekToSeekBar(mThinNoseLevel);
                    if (mOnFaceUnityControlListener != null)
                        mOnFaceUnityControlListener.onThinNoseLevelSelected(mThinNoseLevel);
                }
                if (position == 8) {//嘴型
                    seekToSeekBar(mMouthShape, -50, 50);
                    if (mOnFaceUnityControlListener != null)
                        mOnFaceUnityControlListener.onMouthShapeSelected(mMouthShape);
                }

                RelativeLayout mRlBeauty = (RelativeLayout) adapter.getViewByPosition(rvMh, position, R.id.rl_beauty);
                ImageView IvBeauty = (ImageView) adapter.getViewByPosition(rvMh, position, R.id.iv_beauty);
                mRlBeauty.setBackground(context.getResources().getDrawable(R.drawable.shape_round_white_bg));
                IvBeauty.setImageDrawable(context.getResources().getDrawable(pressBeautyList.get(position)));
                if (currentBPosition != -1 && currentBPosition != position) {
                    RelativeLayout oldmRlBeauty = (RelativeLayout) adapter.getViewByPosition(rvMh, currentBPosition, R.id.rl_beauty);
                    ImageView oldIvBeauty = (ImageView) adapter.getViewByPosition(rvMh, currentBPosition, R.id.iv_beauty);
                    oldmRlBeauty.setBackground(context.getResources().getDrawable(R.drawable.shape_round_white70_bg));
                    oldIvBeauty.setImageDrawable(context.getResources().getDrawable(beautyList.get(currentBPosition)));
                }
                currentBPosition = position;
                mTvDefaultUp.setBackground(context.getResources().getDrawable(R.drawable.shape_round_white70_bg));
                mTvDefaultDown.setTextColor(context.getResources().getColor(R.color.white));

            }
        });


    }

    public interface OnDialogDissMissListener {
        void dissMissClick();
    }

    public void setOnDialogDissMissListener(OnDialogDissMissListener onDialogDissMissListener) {
        this.onDialogDissMissListener = onDialogDissMissListener;
    }

    private void initAdapter() {
        mHsLj.fullScroll(ScrollView.FOCUS_LEFT);
        if (mShortVideoRecorder == null) {
            builtinFilterList = mShortVideoEditor.getBuiltinFilterList();
        } else {
            builtinFilterList = mShortVideoRecorder.getBuiltinFilterList();
        }
        if (filterListAdapter == null) {
            filterListAdapter = new FilterListAdapter();
            filterListAdapter.addData(Arrays.asList(builtinFilterList));
            rvRv.setAdapter(filterListAdapter);
        }
        int beautyFilterLevel = AppUtils.getBeautyFilterLevel();
        if (beautyFilterLevel != -1) {
            beautyFilter = beautyFilterLevel;
            currentFPosition = beautyFilter;
        }
        mBeautySeekBarLayout.setVisibility(View.INVISIBLE);
        filterListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                initFilterAdapter((FilterListAdapter) adapter, position);
                AppUtils.setSelfBeautyLevelClick(-1);
                if (currentFPosition != -1 && currentFPosition != position) {
                    CircleImageView oldIcon = (CircleImageView) adapter.getViewByPosition(rvRv, currentFPosition, R.id.icon);
                    ImageView oldTag = (ImageView) adapter.getViewByPosition(rvRv, currentFPosition, R.id.iv_tag);
                    oldIcon.setBorderWidth(0);
                    oldTag.setVisibility(View.GONE);
                }
                currentFPosition = position;
                mTvDefaultUp.setBackground(context.getResources().getDrawable(R.drawable.shape_round_white70_bg));
                mTvDefaultDown.setTextColor(context.getResources().getColor(R.color.white));
                AppUtils.setBeautyFilterLevel(position);
            }
        });
        mHsLj.setVisibility(VISIBLE);
        mllMeiHua.setVisibility(View.GONE);
        position = 0;
    }

    /**
     * 选择滤镜
     *
     * @param adapter
     * @param position
     */
    private void initFilterAdapter(FilterListAdapter adapter, int position) {
        CircleImageView icon = (CircleImageView) adapter.getViewByPosition(rvRv, position, R.id.icon);
        ImageView tag = (ImageView) adapter.getViewByPosition(rvRv, position, R.id.iv_tag);
        icon.setBorderWidth(3);
        tag.setVisibility(VISIBLE);
        PLBuiltinFilter item = (PLBuiltinFilter) adapter.getItem(position);
        mSelectedFilter = item.getName();
        if (mShortVideoRecorder == null) {
            VideoFilterCommand videoFilterCommand = new VideoFilterCommand(mShortVideoEditor, AppUtils.getBeautyFilterLevel(),position, mSelectedFilter,AppUtils.getBeautyFilterLevel() ==-1?"":builtinFilterList[AppUtils.getBeautyFilterLevel()].getName());
            VideoHelper.getIntence().getCommendFilterManger().pushCommandAndApply(videoFilterCommand);
            ((PreViewActivity)context).initFilterCommandManger();
        } else {
            mShortVideoRecorder.setBuiltinFilter(mSelectedFilter);
        }
    }

    private void changBeautyUI(TextView textView) {
        mTvZone.setTextColor(context.getResources().getColor(R.color.white));
        mTvOne.setTextColor(context.getResources().getColor(R.color.white));
        mTvTwo.setTextColor(context.getResources().getColor(R.color.white));
        mTvThree.setTextColor(context.getResources().getColor(R.color.white));
        mTvFour.setTextColor(context.getResources().getColor(R.color.white));
        mTvFive.setTextColor(context.getResources().getColor(R.color.white));
        mTvZone.setBackground(context.getResources().getDrawable(R.drawable.shape_round_white70_bg));
        mTvOne.setBackground(context.getResources().getDrawable(R.drawable.shape_round_white70_bg));
        mTvTwo.setBackground(context.getResources().getDrawable(R.drawable.shape_round_white70_bg));
        mTvThree.setBackground(context.getResources().getDrawable(R.drawable.shape_round_white70_bg));
        mTvFour.setBackground(context.getResources().getDrawable(R.drawable.shape_round_white70_bg));
        mTvFive.setBackground(context.getResources().getDrawable(R.drawable.shape_round_white70_bg));
        if (position == 2) {
            mTvDefaultDown.setText("使用默认");
            mTvDefaultUp.setBackground(context.getResources().getDrawable(R.drawable.shape_round_white70_bg));
        }
        if (textView == null) return;
        textView.setTextColor(context.getResources().getColor(R.color.black));
        textView.setBackground(context.getResources().getDrawable(R.drawable.shape_round_white_bg));
    }


    public void setAgument(int grade) {
        if (mOnFaceUnityControlListener == null) return;
        mOnFaceUnityControlListener.onBlurLevelSelected(mFaceBeautyBlurLevel);//磨皮
        mOnFaceUnityControlListener.onColorLevelSelected(mFaceBeautyColorLevel);//美白
        mOnFaceUnityControlListener.onBrightEyesSelected(mBrightEyesLevel);//亮眼
        mOnFaceUnityControlListener.onEnlargeEyeSelected(mFaceBeautyEnlargeEye);//大眼
        mOnFaceUnityControlListener.onCheekThinSelected(mFaceBeautyCheekThin);//瘦脸
        mOnFaceUnityControlListener.onChinLevelSelected(mChinLevel);//下巴
        mOnFaceUnityControlListener.onForeheadLevelSelected(mForeheadLevel);//额头
        mOnFaceUnityControlListener.onThinNoseLevelSelected(mThinNoseLevel);//瘦鼻
        mOnFaceUnityControlListener.onMouthShapeSelected(mMouthShape);//嘴型
        if (grade != 0) {
            mOnFaceUnityControlListener.onBeautySkinTypeSelected(1);//精准妹夫
            mOnFaceUnityControlListener.onALLBlurLevelSelected(1);//精准磨皮
//            mOnFaceUnityControlListener.onToothWhitenSelected(1);//精准美呀
            mOnFaceUnityControlListener.onRedLevelSelected(1);//红润
        } else {
            mOnFaceUnityControlListener.onBeautySkinTypeSelected(0);//精准妹夫
            mOnFaceUnityControlListener.onALLBlurLevelSelected(0);//精准磨皮
//            mOnFaceUnityControlListener.onToothWhitenSelected(0);//精准美呀
            mOnFaceUnityControlListener.onRedLevelSelected(0);//红润
        }

    }

    public void setLevel(int grade) {
        AppUtils.setBeautyParameterLevel(grade);
        switch (grade) {
            case -1:
                if (onBeautyNoneListener!=null) {
                    onBeautyNoneListener.onHaveBeautyClick();
                }
                mFaceBeautyBlurLevel = 0.6f;//磨皮
                mFaceBeautyColorLevel = 0.5f;//美白
                mBrightEyesLevel = 0.3f;//亮眼
                mFaceBeautyEnlargeEye = 0.3f;//大眼
                mFaceBeautyCheekThin = 0.5f;//瘦脸
                mChinLevel = 0.42f;//下巴
                mForeheadLevel = 0.40f;//额头
                mThinNoseLevel = 0.4f;//瘦鼻
                mMouthShape = 0.43f;//嘴形

                initBeautyAdapter();
                mTvDefaultUp.setBackground(context.getResources().getDrawable(R.drawable.shape_round_white_bg));
                mTvDefaultDown.setText("默认美化");
                break;
            case 0:
                if (onBeautyNoneListener!=null) {
                    onBeautyNoneListener.onNoneBeautyClick();
                }
                mFaceBeautyBlurLevel = 0;
                mFaceBeautyColorLevel = 0;
                mBrightEyesLevel = 0;
                mFaceBeautyEnlargeEye = 0;
                mFaceBeautyCheekThin = 0;
                mChinLevel = 0;
                mForeheadLevel = 0;
                mThinNoseLevel = 0;
                mMouthShape = 0;
                break;
            case 1:
                if (onBeautyNoneListener!=null) {
                    onBeautyNoneListener.onHaveBeautyClick();
                }
                mFaceBeautyBlurLevel = 0.3f;
                mFaceBeautyColorLevel = 0.2f;
                mBrightEyesLevel = 0;
                mFaceBeautyEnlargeEye = 0.1f;
                mFaceBeautyCheekThin = 0.1f;
                mChinLevel = 0.5f;
                mForeheadLevel = 0.5f;
                mThinNoseLevel = 0;
                mMouthShape = 0.5f;
                break;
            case 2:
                if (onBeautyNoneListener!=null) {
                    onBeautyNoneListener.onHaveBeautyClick();
                }
                mFaceBeautyBlurLevel = 0.5f;
                mFaceBeautyColorLevel = 0.35f;
                mBrightEyesLevel = 0.1f;
                mFaceBeautyEnlargeEye = 0.25f;
                mFaceBeautyCheekThin = 0.3f;
                mChinLevel = 0.5f;
                mForeheadLevel = 0.5f;
                mThinNoseLevel = 0.1f;
                mMouthShape = 0.5f;
                break;
            case 3:
                if (onBeautyNoneListener!=null) {
                    onBeautyNoneListener.onHaveBeautyClick();
                }
                mFaceBeautyBlurLevel = 0.7f;
                mFaceBeautyColorLevel = 0.5f;
                mBrightEyesLevel = 0.5f;
                mFaceBeautyEnlargeEye = 0.4f;
                mFaceBeautyCheekThin = 0.5f;
                mChinLevel = 0.40f;
                mForeheadLevel = 0.40f;
                mThinNoseLevel = 0.2f;
                mMouthShape = 0.5f;
                break;
            case 4:
                if (onBeautyNoneListener!=null) {
                    onBeautyNoneListener.onHaveBeautyClick();
                }
                mFaceBeautyBlurLevel = 0.85f;
                mFaceBeautyColorLevel = 0.7f;
                mBrightEyesLevel = 0.7f;
                mFaceBeautyEnlargeEye = 0.6f;
                mFaceBeautyCheekThin = 0.75f;
                mChinLevel = 0.3f;
                mForeheadLevel = 0.35f;
                mThinNoseLevel = 0.25f;
                mMouthShape = 0.5f;
                break;
            case 5:
                if (onBeautyNoneListener!=null) {
                    onBeautyNoneListener.onHaveBeautyClick();
                }
                mFaceBeautyBlurLevel = 1;
                mFaceBeautyColorLevel = 0.8f;
                mBrightEyesLevel = 0.75f;
                mFaceBeautyEnlargeEye = 0.65f;
                mFaceBeautyCheekThin = 0.8f;
                mChinLevel = 0.3f;
                mForeheadLevel = 0.3f;
                mThinNoseLevel = 0.4f;
                mMouthShape = 0.43f;
                break;
        }
        setAgument(grade);

    }

    private OnFaceUnityControlListener mOnFaceUnityControlListener;

    public void setOnFaceUnityControlListener(@NonNull OnFaceUnityControlListener onFaceUnityControlListener) {
        mOnFaceUnityControlListener = onFaceUnityControlListener;
    }

    public void dismiss() {
        if (dialog != null) {
            dialog.dismiss();
        }
    }


    public class FilterListAdapter extends BaseQuickAdapter<PLBuiltinFilter, BaseViewHolder> {

        public FilterListAdapter() {
            super(R.layout.filter_item);
        }

        @Override
        protected void convert(BaseViewHolder helper, PLBuiltinFilter item) {
            try {
                CircleImageView mIcon = helper.getView(R.id.icon);
                TextView mName = helper.getView(R.id.name);
                ImageView tag = helper.getView(R.id.iv_tag);
                mName.setText(item.getName());
                InputStream is = context.getAssets().open(item.getAssetFilePath());
                Bitmap bitmap = BitmapFactory.decodeStream(is);
                mIcon.setImageBitmap(bitmap);
                mIcon.setBorderColor(context.getResources().getColor(R.color.white));
                if (beautyFilter != -1) {
                    if (helper.getAdapterPosition() == beautyFilter) {
                        mIcon.setBorderWidth(3);
                        tag.setVisibility(VISIBLE);
                        if (position == 0) {
                            mTvDefaultUp.setBackground(context.getResources().getDrawable(R.drawable.shape_round_white70_bg));
                            mTvDefaultDown.setText("无");
                        }
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public class BeautyListAdapter extends BaseQuickAdapter<Integer, BaseViewHolder> {

        public BeautyListAdapter() {
            super(R.layout.item_beauty_adapter);
        }

        @Override
        protected void convert(BaseViewHolder helper, Integer item) {
            RelativeLayout view = helper.getView(R.id.rl_beauty);
            ImageView beauty = helper.getView(R.id.iv_beauty);
            TextView name = helper.getView(R.id.name);
            beauty.setImageDrawable(mContext.getResources().getDrawable(item));
            name.setText(nameList.get(helper.getAdapterPosition()));
            if (AppUtils.getBeautyParameterLevel() != -1) {
                view.setBackground(context.getResources().getDrawable(R.drawable.shape_round_white70_bg));
                beauty.setImageDrawable(context.getResources().getDrawable(beautyList.get(helper.getAdapterPosition())));
            } /*else {
                if (AppUtils.getSelfBeautyLevelClick() != -1) {
                    if (helper.getAdapterPosition() == AppUtils.getSelfBeautyLevelClick()) {
                        view.setBackground(context.getResources().getDrawable(R.drawable.shape_round_white_bg));
                        beauty.setImageDrawable(context.getResources().getDrawable(pressBeautyList.get(helper.getAdapterPosition())));
                    }
                }*/
//            }
        }
    }



    public void setOnBeautyNoneListener(OnBeautyNoneListener onBeautyNoneListener) {
        this.onBeautyNoneListener = onBeautyNoneListener;
    }

}
